﻿

using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;

namespace Traceability.Infaseture
{
    public class BaseRepostoiy<T>:IBaseRepostiy<T>where T : class
    {
        MyDbContext db;

        public BaseRepostoiy(MyDbContext db)
        {
            this.db = db;
        }
        public async Task<int> AddAsync(T a)
        {
        await db.Set<T>().AddAsync(a);
        return await db.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();    
        }
        public async Task<int> Update(T a)
        {
             db.Set<T>().Update(a);
            return await db.SaveChangesAsync();
        }

        public async Task<int> Delete(T a)
        {
            db.Set<T>().Remove(a);
            return await db.SaveChangesAsync();
        }

        public async Task<int> Add(T a)
        {
            db.Set<T>().Add(a);
            return  db.SaveChanges();
        }

        public async Task<T> GetValues(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().Where(expression).FirstOrDefaultAsync();
        }
    }
}
